package com.wind.taobao.api.trade.item.convert
{
	import com.wind.taobao.api.trade.item.model.Sku;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * Sku转换器
	 * @author fxy
	 */
	public class SkuConvert
	{
		/**
		 * 转换SKU集合
		 */
		public static function convertSkus(value:Object, type:String="json"):ArrayCollection
		{
			var skus:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							skus.addItem(convertSku((value as Array)[i]));
						}
					}
				}
				else if (value is XMLList)
				{
					//TODO
				}
			}
			return skus;
		}

		/**
		 * 转换SKU
		 */
		public static function convertSku(value:Object, type:String="json"):Sku
		{
			var sku:Sku=new Sku();
			if (value == null)
				return sku;
			if (type == TaobaoConstants.XML)
			{
				sku=convertXMLToSku(XML(value), sku);
			}
			else
			{
				sku=convertJSONToSku(value, sku);
			}
			return sku;
		}

		/**
		 * JSON转换为SKU
		 */
		private static function convertJSONToSku(json:Object, sku:Sku):Sku
		{
			if (json.hasOwnProperty(TaobaoConstants.SKU_ID))
			{
				sku.skuId=json[TaobaoConstants.SKU_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.EXTRA_ID))
			{
				sku.extraId=json[TaobaoConstants.EXTRA_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.IID))
			{
				sku.iid=json[TaobaoConstants.IID];
			}
			if (json.hasOwnProperty(TaobaoConstants.PROPERTIES))
			{
				sku.properties=json[TaobaoConstants.PROPERTIES];
			}
			if (json.hasOwnProperty(TaobaoConstants.QUANTITY))
			{
				sku.quantity=json[TaobaoConstants.QUANTITY];
			}
			if (json.hasOwnProperty(TaobaoConstants.PRICE))
			{
				sku.price=json[TaobaoConstants.PRICE];
			}
			if (json.hasOwnProperty(TaobaoConstants.OUTER_ID))
			{
				sku.outerId=json[TaobaoConstants.OUTER_ID];
			}
			if (json.hasOwnProperty(TaobaoConstants.STATUS))
			{
				sku.status=json[TaobaoConstants.STATUS];
			}
			if (json.hasOwnProperty(TaobaoConstants.CREATED))
			{
				sku.created=json[TaobaoConstants.CREATED];
			}
			if (json.hasOwnProperty(TaobaoConstants.MODIFIED))
			{
				sku.modified=json[TaobaoConstants.MODIFIED];
			}
			return sku;
		}

		/**
		 * XML转换为SKU
		 */
		private static function convertXMLToSku(value:XML, sku:Sku):Sku
		{
			return sku;
		}
	}
}